package com.wxg.service;

import java.text.SimpleDateFormat;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.wxg.domain.englishword.Englishword;
import com.wxg.domain.englishword.Wordexplain;
import com.wxg.httpclient.ObtainAudioFromBaiduFanyi.AudioType;
import com.wxg.jfinal.util.JFinalUtils;
import com.wxg.sqlite.util.Sql;

public class EnglishWordService {
	
	private static final String PATTERN = "yyyy-MM-dd HH:mm:ss.SSS";
	
	public static final SimpleDateFormat SDF = new SimpleDateFormat(PATTERN);
	
	static {
		JFinalUtils.startNonWeb();
	}
	
	/**
	 * 
	 * 根据"单词名称"从数据库查询一条记录
	 * 
	 * @param word
	 * @return
	 */
	public Englishword findByWord(String word){
		Sql sql = new Sql();
		sql.add("select * from ");
		sql.heel(JFinalUtils.getTable(Englishword.class).getName());
		sql.add(" where word = ");
		sql.quotation(word);
		return Englishword.dao.findFirst(sql.str());
	}
	
	/**
	 * 
	 * 根据"单词名称"从数据库查询单词释义！
	 * 
	 * @param word
	 * @return
	 */
	public List<Wordexplain> findExplainsByWord(String word){
		Sql sql = new Sql();
		sql.add("select * from ");
		sql.heel(JFinalUtils.getTable(Wordexplain.class).getName());
		sql.add(" where word = ");
		sql.quotation(word);
		return Wordexplain.dao.find(sql.str());
	}

	@Before(Tx.class)
	public void add(Englishword word, List<Wordexplain> explainList){
		word.setId(null);
		word.setCreateTime(currentTime());
		
		Wordexplain justMark = null;
		
		boolean flag = word.save();
		if(flag){
			Integer id = word.getId();
			for(Wordexplain explain : explainList){
				explain.setId(null);
				explain.setWordId(id);
				explain.setWord(word.getWord());
				flag = explain.save();
				if(!flag){
					justMark = explain;
					break;
				}
			}
		}else{
			throw new RuntimeException(String.format("save failed!: %s", word.toJson()));
		}
		if(!flag){
			throw new RuntimeException(String.format("save failed!: %s", justMark.toJson()));
		}
	}
	
	/**
	 * 
	 * @param word
	 * @return 数据库表主键
	 */
	@Before(Tx.class)
	public Integer addWord(Englishword word){
		word.setId(null);
		word.setCreateTime(currentTime());

		Integer returnId = null;
		
		boolean flag = word.save();
		if(flag){
			returnId = word.getId();
		}else{
			throw new RuntimeException(String.format("save failed!: %s", word.toJson()));
		}
		
		return returnId;
	}
	
	@Before(Tx.class)
	public Englishword addWord(String word0){
		Englishword word = new Englishword();
		word.setWord(word0);
		word.setId(null);
		word.setCreateTime(currentTime());

		if(!word.save()){
			throw new RuntimeException(String.format("save failed!: %s", word.toJson()));
		}
		return word;
	}
	
	/**
	 * Add at 20170514 15:55
	 * 之前修改代码时，没有传递path，导致数据库表中字段没值！
	 * 
	 * @param word0
	 * @param path
	 * @param type
	 * @return
	 */
	@Before(Tx.class)
	public Englishword addWord(String word0,String path, AudioType type){
		Englishword word = new Englishword();
		word.setWord(word0);
		if(type.equals(AudioType.USA)){
			word.setUsaPath(path);
		}
		if(type.equals(AudioType.Britain)){
			word.setBritainPath(path);
		}
		word.setId(null);
		word.setCreateTime(currentTime());

		if(!word.save()){
			throw new RuntimeException(String.format("save failed!: %s", word.toJson()));
		}
		return word;
	}
	
	/**
	 * 
	 * @param explainList
	 */
	@Before(Tx.class)
	public void addExplains(List<Wordexplain> explainList){
		Wordexplain justMark = null;
		
		boolean flag = false;
		for(Wordexplain explain : explainList){
			explain.setId(null);
			flag = explain.save();
			if(!flag){
				justMark = explain;
				break;
			}
		}
		
		if(!flag){
			throw new RuntimeException(String.format("save failed!: %s", justMark.toJson()));
		}
	}
	
	
	@Before(Tx.class)
	public void delById(Integer id){
		Englishword word = Englishword.dao.findById(id);
		if(null == word){
			throw new RuntimeException(String.format("Englishword(id=%s) is not exist!", id));
		}
		List<Wordexplain> list = Wordexplain.dao.findByWordid(id);
		if(null == list){
			throw new RuntimeException(String.format("Wordexplain(wordId=%s) is not exist!", id));
		}
		boolean flag = false;
		Wordexplain justMark = null;
		for(Wordexplain explain : list){
			flag = explain.delete();
			if(!flag){
				justMark = explain;
				break;
			}
		}
		if(!flag){
			throw new RuntimeException(String.format("Wordexplain delete failed! %s", justMark.toJson()));
		}else{
			flag = word.delete();
		}
		if(!flag){
			throw new RuntimeException(String.format("Englishword delete failed! %s", word.toJson()));
		}
	}
	
	
	
	
	private String currentTime(){
		return SDF.format(new java.util.Date());
	}
}
